package com.material.contract.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.material.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.material.common.annotation.Log;
import com.material.common.core.controller.BaseController;
import com.material.common.core.domain.AjaxResult;
import com.material.common.enums.BusinessType;
import com.material.contract.domain.ContractTemplate;
import com.material.contract.service.IContractTemplateService;
import com.material.common.utils.poi.ExcelUtil;
import com.material.common.core.page.TableDataInfo;

/**
 * 合同模板Controller
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/contract/template")
public class ContractTemplateController extends BaseController
{
    @Autowired
    private IContractTemplateService contractTemplateService;

    /**
     * 查询合同模板列表
     */
    @PreAuthorize("@ss.hasPermi('contract:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContractTemplate contractTemplate)
    {
        Long bizId = getBizId();
        boolean admin = SecurityUtils.isAdmin(getUserId());
        if(!admin){
            contractTemplate.setFactoryId(bizId);
        }
        startPage();
        List<ContractTemplate> list = contractTemplateService.selectContractTemplateList(contractTemplate);
        return getDataTable(list);
    }

    /**
     * 导出合同模板列表
     */
    @PreAuthorize("@ss.hasPermi('contract:template:export')")
    @Log(title = "合同模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContractTemplate contractTemplate)
    {
        List<ContractTemplate> list = contractTemplateService.selectContractTemplateList(contractTemplate);
        ExcelUtil<ContractTemplate> util = new ExcelUtil<ContractTemplate>(ContractTemplate.class);
        util.exportExcel(response, list, "合同模板数据");
    }

    /**
     * 获取合同模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('contract:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(contractTemplateService.selectContractTemplateById(id));
    }

    /**
     * 新增合同模板
     */
    @PreAuthorize("@ss.hasPermi('contract:template:add')")
    @Log(title = "合同模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContractTemplate contractTemplate)
    {
        Long bizId = getBizId();
        boolean admin = SecurityUtils.isAdmin(getUserId());
        if(!admin){
            contractTemplate.setFactoryId(bizId);
        }
        return toAjax(contractTemplateService.insertContractTemplate(contractTemplate));
    }

    /**
     * 修改合同模板
     */
    @PreAuthorize("@ss.hasPermi('contract:template:edit')")
    @Log(title = "合同模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContractTemplate contractTemplate)
    {
        return toAjax(contractTemplateService.updateContractTemplate(contractTemplate));
    }

    /**
     * 删除合同模板
     */
    @PreAuthorize("@ss.hasPermi('contract:template:remove')")
    @Log(title = "合同模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(contractTemplateService.deleteContractTemplateByIds(ids));
    }
}
